﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class AdDAL
    {
        public static int Save(AdInfo info)
        {
            string sql = string.Format("insert into [ad]([AdminID],[SID],[Name],[Img],[Link],[Position],[Sort],[ClickCount],[Froms],[Context],[CreateDate]) values(@AdminID,@SID,@Name,@Img,@Link,@Position,@Sort,@ClickCount,@Froms,@Context,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@SID", info.SID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Img", info.Img),
                new SqlParameter("@Link", info.Link),
                new SqlParameter("@Position", info.Position),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@ClickCount", info.ClickCount),
                new SqlParameter("@Froms", info.Froms),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(AdInfo info)
        {
            string sql = string.Format("update [ad] set [AdminID]=@AdminID,[SID]=@SID,[Name]=@Name,[Img]=@Img,[Link]=@Link,[Position]=@Position,[Sort]=@Sort,[Froms]=@Froms,[Context]=@Context where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@SID", info.SID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Img", info.Img),
                new SqlParameter("@Link", info.Link),
                new SqlParameter("@Position", info.Position),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@Froms", info.Froms),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static List<AdInfo> FindTop(int adminid, int top, int position)
        {
            StringBuilder filter = new StringBuilder();
            if (position != 0)
            {
                filter.AppendFormat(" and Position={0}", position);
            }
            DataTable dt = CommonDAL.GetDataTable(string.Format("select top {0} * from [ad] where AdminID={1} {2} order by Sort",top, adminid, filter.ToString()));
            return ListSimpleData(dt);
        }
        public static AdInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[ad]");
            return SimpleData(dr);
        }
        public static bool IncClickCount(int id)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [ad] set ClickCount=ClickCount+1 where ID={0}", id));
        }
        public static List<AdInfo> FindPage(string keyword, int adminid, string begindate,string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            filter.AppendFormat(" and [ad].AdminID={0}", adminid);
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [ad].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [ad].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[ad]", out count);
            return ListSimpleData(dt);
        }

        public static List<AdInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [ad]"));
            return ListSimpleData(dt);
        }
        private static AdInfo SimpleData(DataRow dr)
        {
            AdInfo info = new AdInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Img = Commons.domain + DataConvert.ToString(dr["Img"]);
                info.Link = DataConvert.ToString(dr["Link"]);
                info.Position = DataConvert.ToInt(dr["Position"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.ClickCount = DataConvert.ToInt(dr["ClickCount"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.Froms = DataConvert.ToInt(dr["Froms"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateDateText = info.CreateDate.ToString("yyyy-MM-dd");
            }
            return info;
        }
        private static AdInfo AllData(DataRow dr)
        {
            AdInfo info = new AdInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Img = DataConvert.ToString(dr["Img"]);
                info.Link = DataConvert.ToString(dr["Link"]);
                info.Position = DataConvert.ToInt(dr["Position"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.ClickCount = DataConvert.ToInt(dr["ClickCount"]);
                info.Froms = DataConvert.ToInt(dr["Froms"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateDateText = info.CreateDate.ToString("yyyy-MM-dd");
            }
            return info;
        }
        private static List<AdInfo> ListSimpleData(DataTable dt)
        {
            List<AdInfo> list = new List<AdInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<AdInfo> ListAllData(DataTable dt)
        {
            List<AdInfo> list = new List<AdInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}
